package com.lyl.syt.modules.mongodb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyl.syt.client.DictFeignClient;
import com.lyl.syt.common.exception.YyghException;
import com.lyl.syt.common.result.ResultCodeEnum;
import com.lyl.syt.model.hosp.Hospital;
import com.lyl.syt.model.hosp.HospitalSet;
import com.lyl.syt.modules.mongodb.dao.HospitalDao;
import com.lyl.syt.modules.mongodb.service.HospitalService;
import com.lyl.syt.vo.hosp.HospitalQueryVo;
import com.lyl.syt.vo.order.SignInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class HospitalServiceImpl implements HospitalService {
    private final HospitalDao hospitalDao;
    private final DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //将map转换成对象
        String json = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(json, Hospital.class);

        //判断是否存在数据
        Hospital hospitalExit = hospitalDao.findByHoscode(hospital.getHoscode());

        //存在则修改数据，不存在则添加数据
        if (hospitalExit != null) {
            hospital.setStatus(hospitalExit.getStatus());
            hospital.setCreateTime(hospitalExit.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        } else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        }
        hospitalDao.save(hospital);
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalDao.findByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> getHospPage(int page, int limit, HospitalQueryVo queryVo) {
        //分页查询
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //将查询条件赋值给hospital
        Hospital hospital = new Hospital();
        BeanUtil.copyProperties(queryVo, hospital);

        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> hospitalPage = hospitalDao.findAll(example, pageRequest);

        //设置
        List<Hospital> hospitalList = hospitalPage.getContent();
        hospitalList.forEach(this::setHosType);

        return hospitalPage;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalDao.findById(id).get();
        //设置状态
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalDao.save(hospital);
    }

    @Override
    public Map<String, Object> getById(String id) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = setHosType(hospitalDao.findById(id).get());
        //医院基本信息
        result.put("hospital", hospital);
        //预约信息
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalDao.findByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        Map<String, Object> resultMap = new HashMap<>();

        //医院详情
        Hospital hospital = setHosType(getByHoscode(hoscode));
        resultMap.put("hospital", hospital);

        //预约规则
        resultMap.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return resultMap;
    }

    private Hospital setHosType(Hospital hospital) {
        //获取医院等级名称
        String hostype = dictFeignClient.getName("hostype", hospital.getHostype());
        hospital.getParam().put("hostypeString", hostype);

        //获取省名称
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        //获取市
        String city = dictFeignClient.getName(hospital.getCityCode());
        //获取地址
        String district = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress", province + city + district);

        return hospital;
    }
}
